<template>
  <div id="login" v-title data-title="登录 - 简易论坛">

    <div class="me-login-box me-login-box-radius">
      <h1>简易论坛 登录</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="用户名" v-model="userForm.username"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="userForm.password"></el-input>
        </el-form-item>

        <el-form-item prop="captcha">
          <el-row :gutter="20">
            <el-col :span="14">
              <el-input v-model="userForm.captcha" placeholder="验证码">
              </el-input>
            </el-col>
            <el-col :span="10" class="login-captcha">
              <img :src="captchaPath" @click="getCaptcha()" alt="">
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click="dataFormSubmit()">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
  import { getUUID } from '../utils/validate'

  export default {
    name: 'Login',
    data() {
      return {
        userForm: {
          username: '',
          password: '' ,
          uuid: '' ,
          captcha: '' ,
        },
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {max: 10, message: '不能大于10个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {max: 20, message: '不能大于20个字符', trigger: 'blur'}
          ] ,
          captcha: [
            { required: true, message: '验证码不能为空', trigger: 'blur' }
          ]
        } ,
        captchaPath: '' ,
      }
    },
    methods: {
      dataFormSubmit() {

        this.$refs['userForm'].validate((valid) => {
          if (valid) {
            console.log(this.userForm)
            // 登录请求发送
            this.$store.dispatch('loginAction', this.userForm)
            .then(() => {
                // 跳转回首页
                this.$router.back()
            }).catch((error) => {
              if (error !== 'error') {
                this.$message({
                  message: error, 
                  type: 'error', 
                  showClose: true
                });
              }
            })
          } else {
            return false;
          }
        });
      } ,
      // 获取验证码
      getCaptcha () {
        this.userForm.uuid = getUUID()
        this.captchaPath = this.$service.adornUrl(`/user/forumUser/captcha.jpg?uuid=${this.userForm.uuid}`)
      } ,
    } ,
    created () {
      this.getCaptcha()
    },
  }
</script>

<style scoped>
  #login {
    min-width: 100%;
    min-height: 100%;
  }

  .me-video-player {
    background-color: transparent;
    width: 100%;
    height: 100%;
    object-fit: fill;
    display: block;
    position: absolute;
    left: 0;
    z-index: 0;
    top: 0;
  }

  .me-login-box {
    position: absolute;
    width: 300px;
    height: 260px;
    background-color: white;
    margin-top: 150px;
    margin-left: -180px;
    left: 50%;
    padding: 30px;
  }

  .me-login-box-radius {
    border-radius: 10px;
    box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
  }

  .me-login-box h1 {
    text-align: center;
    font-size: 24px;
    margin-bottom: 20px;
    vertical-align: middle;
  }

  .me-login-design {
    text-align: center;
    font-family: 'Open Sans', sans-serif;
    font-size: 18px;
  }

  .me-login-design-color {
    color: #5FB878 !important;
  }

  .me-login-button {
    text-align: center;
  }

  .me-login-button button {
    width: 100%;
  }

  .login-captcha {
    overflow: hidden;
  }

  .login-captcha img {
    width: 100%;
    cursor: pointer;
  }

</style>
